
var downInfo={
    fileName:"",
    time:"",
    url:""
}
var ttime;

function getDownHistory(){
    return sessionStorage.getItem("downHistory");
}
    function setDownHistoroy(item){

           var downHistory = getDownHistory();
           if(downHistory==null){
               var arr = new Array();
               arr.unshift(item);

           }else{
               var arr = JSON.parse(downHistory);
               console.log("获取历史记录",arr);
               arr.unshift(item);

           }
           console.log(JSON.stringify(arr))
           sessionStorage.setItem("downHistory",JSON.stringify(arr));

    }




    $("#loginBtn").click(function (){

        $("#login").modal("show");
    })

    $("#loginSubmit").click(function (){
        var Aname = $("#Aname").val();
        var Apwd = $("#Apwd").val();

        if(Aname == "" || Apwd == "")  return;
        $.ajax({
            url:"/login",
            type: "post",
            dataType: "json",
            data: {
                'Aname':Aname,
                'Apwd':Apwd
            },
            success(res) {
                console.log(res)
               if(res.code==200){
                   window.location.href="/admin"
               }else if(res.code == 300) {
                   alert("用户名或密码不正确！");
               }
            }
            }
        )
    })
    //下载提交数据
    $("#submit").click(function (){
        console.log("提交任务")
        var url = $("#url").val();
        var verityCode = $("#verityCode").val();
        console.log("verity"+verityCode)
        url = url.trim();
        console.log(url);
        if(url.indexOf("https://github.com")!=0){
            $("#res").text("链接不正确");
            $("#res").css("color","red")
            return;
        }
        var time = down_process();
        showModal();

                $.ajax({
                    url:"/download",
                    async:true,
                    dataType : 'json',
                    type:"post",
                    data:{'url':url,
                          'verityCode':verityCode
                         },
                    success:function (res){
                        clearInterval(time);
                        console.log(res)
                        clearInterval(ttime);
                        if(res.code==200){

                            $("#res").text(res.msg);
                            $('#qrcode').modal('hide');
                            $('#downUrl').text("点我下载");
                            $('#downUrl').css("display","block");//显示div
                            $('#downUrl').attr('href',res.data.fileName);

                            var time = fromatTime(new Date());
                            downInfo.fileName=res.data.fileName.substring(res.data.fileName.indexOf("fileName=")+9);
                            downInfo.time = time;
                            downInfo.url = res.data.fileName;
                            setDownHistoroy(downInfo);
                            updateDownloadList();
                        }else{

                            $('#qrcode').modal('hide');
                            $("#res").css("color","red");
                            $("#res").text(res.msg);

                        }

                    }
                })

    })

    function fromatTime(time) {
        var date = time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate() +" "+ time.getHours()+ ":" + time.getMinutes()
        return date
    }

    function down_process(){
        console.log("start")
        ttime = setInterval(function (){
            console.log("process")
            $.ajax({
                url:"/download/process",
                dataType : 'json',
                type:"get",
                success:function (res) {
                    if(res.code==200){
                        $("#process").text("已下载"+res.data);
                    }else{

                    }


                }
            })


        },1000)
        console.log("end");
        return ttime;
    }


       function showModal(){
            $('#qrcode').modal('show');//展示
           //  $('#qrcode').on('show.bs.modal', function (event) {//模型框显示后，可以定义里面的值，这个不是动态的值，用处不大
           //     var modal = $(this);  //get modal itself
           //     modal.find('.modal-body #message').text('请稍等,请勿刷新页面');
           //
           // });
        };
function changeMsg() {
     $('#qrcode').on('show.bs.modal', function (event) {//模型框显示后，可以定义里面的值，这个不是动态的值，用处不大
        var modal = $(this);  //get modal itself
        modal.find('.modal-body #message').text('请稍等,请勿刷新页面');

    });
}



